package com.example.the_breakfast_club;

import android.util.Pair;

/**
 * Currently unused faux-database.
 * @author Team 19
 */
public class PseudoDatabase {

	private Pair<String, String>[] accounts;
	private User[] users;
	private int numUsers;
	private Item[] items;
	private int numItems;
	
	/**
	 * Constructor for the database.
	 */
	//TODO: Fix (?): Every time app starts, new pseudoDatabase
	public PseudoDatabase(){
		accounts = (Pair<String, String>[]) new Pair[100];
		users = new User[100];
		numUsers = 0;
		// TODO: Remove this code. Hardcoding in one user.
		createUser("smahfouz3@gatech.edu", "password");
	}
	
	/**
	 * Adds a username, password pair into the accounts array.
	 * @param username
	 * @param password
	 */
	private void createAccount(String username, String password){
		accounts[numUsers] = new Pair<String, String>(username, password);
		return;
	}
	
	/**
	 * Deletes the pair from the array. (Needs to be completed)
	 * @param username
	 * @param password
	 * @return whether or not the account existed and was successfully removed
	 */
	private boolean deleteAccount(String username, String password){
		boolean success = false;
		//check to see if the pair exists in the accounts array
		//if yes, remove from array (true)
		//if no, error message (false)
		return success;
	}
	
	/**
	 * Checks to see if account already exists.
	 * @return True if exists, false otherwise.
	 */
	public boolean accountExist(String name){
		for (int i = 0; i < numUsers; i++) {
			if ((accounts[i].first).equals(name))
				return true;
		}
		return false;
	}
	
	/**
	 * Creates a user and adds to the array.
	 * @param username
	 * @param password
	 */
	public void createUser(String username, String password){ 
		//check to see if username already exists in accounts[]
		//if false, error message "Username already exists"
		if (accountExist(username))
			return;
		//if true,
		createAccount(username, password);
		users[numUsers] = new User(username, password);
		numUsers++;
		return;
	}
	
	/**
	 * Makes sure the password and username match.
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean verifyPassword(String username, String password){
		for (int i = 0; i < numUsers; i++) {
			if ((accounts[i].first).equals(username) && ((accounts[i].second).equals(password)))
				return true;
		}
		return false;
	}
	
	/**
	 * Creates an item and adds to the array.
	 * @param name
	 * @param description
	 * @param category
	 */
	public void createItem(String name, String description, int category){
		items[numItems] = new Item(name, description, category);
		numItems++;
		return;
	}
}
